package com.example.administrator.dc.Activity;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.example.administrator.dc.Bean.Collectlistresult;
import com.example.administrator.dc.Bean.DataEntity;
import com.example.administrator.dc.Bean.Usermessage;
import com.example.administrator.dc.R;
import com.example.administrator.dc.Service.CollectService;
import com.example.administrator.dc.Service.UserService;
import com.example.administrator.dc.adapter.CollectlistAdapter;
import com.example.administrator.dc.model.CollectModel;
import com.example.administrator.dc.model.Contants;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class CollectActivity extends BaseActivity {
    private RecyclerView recyclerView;
    public List<DataEntity> collectlist;
    private  CollectlistAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collect);
        init();
        Connect();
    }
    private void init(){
        recyclerView= (RecyclerView) findViewById(R.id.CollectRecycle);
    }
    private void Connect(){
       SharedPreferences sp = getApplication().getSharedPreferences("UserInfo", getApplication().MODE_PRIVATE);
        String userid =sp.getString("user_id", null);
        Retrofit retrofit= new Retrofit.Builder().baseUrl(Contants.BASE_URL1).
                addConverterFactory(GsonConverterFactory.create()).build();
        CollectService service=retrofit.create(CollectService.class);
        Call<Collectlistresult> call= service.collectlist(userid);
        call.enqueue(new Callback<Collectlistresult>() {
            @Override
            public void onResponse(Call<Collectlistresult> call, Response<Collectlistresult> response) {
                if(response.body().getStatus()==1){
                    collectlist=response.body().getData();
                    adapter=new CollectlistAdapter(CollectActivity.this,collectlist);
                    LinearLayoutManager layoutManager=new LinearLayoutManager(CollectActivity.this);
                  //  layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                    GridLayoutManager gm = new GridLayoutManager(CollectActivity.this,2);
                    // 网格是的list布局   2列
                    // recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
                    recyclerView.setLayoutManager(gm);
                    // recyclerView.setAdapter(mAdapter);
                    // header.attachTo(recyclerView);
                  //  recyclerView.setLayoutManager(layoutManager);
                    recyclerView.setAdapter(adapter);
                    recyclerView.setItemAnimator(new DefaultItemAnimator());



                }

            }

            @Override
            public void onFailure(Call<Collectlistresult> call, Throwable t) {

            }
        });


    }
}
